package cn.net.ecode.modules.msg.service;

import javax.jms.MapMessage;
import javax.jms.Message;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 消息的异步被动接收者.
 * 
 * 使用Spring的MessageListenerContainer侦听消息并调用本Listener进行处理.
 * 
 * @author ThinkGem
 */
public class MessageReceiver implements javax.jms.MessageListener {

	private static Logger logger = LoggerFactory.getLogger(MessageReceiver.class);

	/**
	 * MessageListener回调函数.
	 */
	@Override
	public void onMessage(Message message) {
		try {
			
			MapMessage mapMessage = (MapMessage) message;
			
			logger.info("model: " + mapMessage.getJMSDeliveryMode());
			logger.info("destination: " + mapMessage.getJMSDestination());
			logger.info("type: " + mapMessage.getJMSType());
			logger.info("messageId: " + mapMessage.getJMSMessageID());
			logger.info("time: " + mapMessage.getJMSTimestamp());
			logger.info("expiredTime: " + mapMessage.getJMSExpiration());
			logger.info("priority: " + mapMessage.getJMSPriority());
			
			// 打印消息详情
			logger.info("title: {}  content: {}", mapMessage.getString("title"), mapMessage.getString("content"));
			
//			mapMessage.acknowledge();
		} catch (Exception e) {
			logger.error("处理消息时发生异常.", e);
		}
	}
}
